cmake_minimum_required(VERSION 3.4.1)
## options
#set(CMAKE_CXX_STANDARD 14)
set(CMAKE_VERBOSE_MAKEFILE ON)
project(libsCommon)
set(TargetName "VmiCommon")
set( CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined" )
set(NORMAL_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -fstack-protector-all" )
set(CMAKE_SKIP_RPATH TRUE)
set(CMAKE_CXX_FLAGS "${NORMAL_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
#add_definitions(-std=c++14)
include_directories(${REPO_ROOT_DIR}/unpack_open_source/swiftshader/include)
include_directories(${THIRDPARTY_DIR})
include_directories(${THIRDPARTY_DIR}/cjson)
include(env.cmake) # defines LIBS_COMMON_DIR THIRDPARTY_DIR
include(ExternalProject)

function(build_codes_linux arg)
    ExternalProject_Add(
        ${ARGV0}
        SOURCE_DIR     ${REPO_ROOT_DIR}/${ARGV1}/${ARGV0}
        BINARY_DIR ${CMAKE_BINARY_DIR}/libs_Common/${ARGV0}
        INSTALL_COMMAND ""
      )
endfunction()

function(build_open_source_android arg)
    ExternalProject_Add(
        libs_Common_${ARGV0}
        SOURCE_DIR    ${REPO_ROOT_DIR}/${ARGV1}/${ARGV0}
        BINARY_DIR ${CMAKE_BINARY_DIR}/libs_Common/${ARGV0}
        CMAKE_COMMAND ${CMAKE_ROOT}/../../bin/cmake
        CMAKE_GENERATOR "Android Gradle - Ninja"
        CMAKE_ARGC -DCMAKE_MAKE_PROGRAM=${CMAKE_ROOT}/../../bin/ninja -DANDROID_ABI=armeabi-v7a -DANDROID_TOOLCHAIN=clang -DVMI_OUTPUT_DIR=${VMI_OUTPUT_DIR} -DCMAKE_TOOLCHAIN_FILE=${CMAKE_ROOT}/../../../../android-ndk-r16b/build/cmake/android.toolchain.cmake
        BUILD_COMMAND ${CMAKE_ROOT}/../../bin/ninja
        INSTALL_COMMAND ""
      )
endfunction()

message(STATUS "========================>System name is ${CMAKE_SYSTEM_NAME}")
if(${CMAKE_SYSTEM_NAME} MATCHES "Android")
    if(NOT TARGET VmiHWSecure)
    build_open_source_android(HWSecure open_source)
    endif()
    build_open_source_android(SQLite open_source)
else()
    message(STATUS "========================>0000")
    if(NOT TARGET VmiHWSecure)
    message(STATUS "========================>1111")
    build_codes_linux(HWSecure open_source)
    endif()
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++")
endif()
link_directories(${CMAKE_BINARY_DIR}/libs_Common/HWSecure)
link_directories(${VMI_OUTPUT_DIR}/armeabi-v7a)

set(Srcs
    Log/logging.cpp
    MemoryManagement/VmiBuffer.cpp
    OSLib/VmiThread.cpp
    OSLib/VmiTimer.cpp
    Communication/InterThreadComm.cpp
    Communication/PacketManager.cpp
    Communication/PacketQueue.cpp
    Communication/Heartbeat/Heartbeat.cpp
    Communication/StreamParse/StreamParser.cpp
    Communication/MsgFragment/MsgReassemble.cpp
    Communication/MsgFragment/MsgFragment.cpp
    Communication/Socket/VmiSocket.cpp
    Communication/Socket/TcpSocket/VmiTcpServerSocket.cpp
    Communication/Socket/TcpSocket/VmiTcpSocket.cpp
    Communication/Socket/TcpSocket/VmiTcpClientSocket.cpp
    Communication/NetComm/NetComm.cpp
    Communication/PacketHandle/PacketHandle.cpp
    ./Utils/BufferReader.cpp
    ./Utils/BufferWriter.cpp
    ./Utils/VmiUtil.cpp
)

if(${CMAKE_SYSTEM_NAME} MATCHES "Android")
    set(Srcs ${Srcs} Utils/Util.cpp)
endif()
if(${CMAKE_VIDEOAGENT_NAME} MATCHES "VideoAgent")
add_library(${TargetName} STATIC ${Srcs})
#target_compile_features(${TargetName} PUBLIC cxx_std_17)
add_dependencies(${TargetName}
    VmiHWSecure
)
else()
add_library(${TargetName} SHARED ${Srcs})
if(VMI_OUTPUT_DIR)
set_target_properties(${TargetName} PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY ${VMI_OUTPUT_DIR}/armeabi-v7a
    )
endif()
#target_compile_features(${TargetName} PUBLIC cxx_std_17)
add_dependencies(${TargetName}
    libs_Common_HWSecure
    libs_Common_SQLite
)
endif()

if(${CMAKE_SYSTEM_NAME} MATCHES "Android")
    target_link_libraries(${TargetName} PUBLIC
        log
    )
else()
    #target_compile_features(${TargetName} PUBLIC cxx_std_17)
    target_link_libraries(${TargetName} PUBLIC
	-ldl
        pthread
    )
endif()

if(${CMAKE_VIDEOAGENT_NAME} MATCHES "VideoAgent")
target_link_libraries(${TargetName} PUBLIC
    VmiHWSecure
)
else()
target_link_libraries(${TargetName} PUBLIC
    VmiHWSecure
    VmiSQLite
)
endif()

target_include_directories(${TargetName} PUBLIC
    ${LIBS_COMMON_DIR}
    ${THIRDPARTY_DIR}
    )
set_target_properties(${TargetName} PROPERTIES POSITION_INDEPENDENT_CODE ON)


include(CMakePrintHelpers)
cmake_print_properties(
    TARGETS ${TargetName}
    PROPERTIES INCLUDE_DIRECTORIES
    )

#install(TARGETS ${TargetName}
#    LIBRARY DESTINATION ${INSTALL_PATH}/libs
#)
